import store from '@/store'

function hasPermission(perms) {
  const permissions = store.state.user.perms
  const index = permissions.indexOf(perms)
  if (index > -1) {
    return true
  } else {
    return false
  }
}

export default {
  inserted(el, binding) {
    const permission = binding.value // 获取到 v-permission的值
    if (permission) {
      const hasPermissions = hasPermission(permission)
      if (!hasPermissions) { // 没有权限 移除Dom元素
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
    // checkPermission(el, binding)
  },
  update(el, binding) {
    // checkPermission(el, binding)
    const permission = binding.value // 获取到 v-permission的值
    if (permission) {
      const hasPermissions = hasPermission(permission)
      if (!hasPermissions) { // 没有权限 移除Dom元素
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  }
}
